package yxy.game.pm2.command.friend;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.FriendMessage;
import yxy.game.pm2.module.friend.FriendModule;

/** 好友 一键拒绝 */
@MessageType(proto = Command.FriendRefuseOneButton, classtype = FriendMessage.FriendRefuseOneButtonRequest.class)
public class CMD_FriendRefuseOneButton extends Command<FriendMessage.FriendRefuseOneButtonRequest> {

	@Override
	public void execute(MessageSource messageSource, FriendMessage.FriendRefuseOneButtonRequest request) {
		String playerCode = messageSource.getTag();

		FriendMessage.FriendRefuseOneButtonResponse.Builder response = FriendMessage.FriendRefuseOneButtonResponse.newBuilder();

		FriendModule friendModule = FriendModule.get(playerCode);
		try {
			if (friendModule.tryLock()) {
				friendModule.getApply().clear();
				friendModule.set();

				send(messageSource, response, Result.Success);
			}
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		} finally {
			friendModule.unlock();
		}
	}

	private void send(MessageSource messageSource, FriendMessage.FriendRefuseOneButtonResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}